package de.unidue.langtech.teaching.pp.example;

import java.util.Collection;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import de.unidue.langtech.teaching.pp.type.TimexEvent;

public class Evaluator extends JCasAnnotator_ImplBase {

	private int correct;
	
	public int timexCounter;
	
	public int timeCounter;
	public int setCounter;
	public int durationCounter;
	public int dateCounter;
	
	public int timeCorrect;
	public int setCorrect;
	public int durationCorrect;
	public int dateCorrect;

	/*
	 * This is called BEFORE any documents are processed.
	 */
	@Override
	public void initialize(UimaContext context)
			throws ResourceInitializationException {
		super.initialize(context);
		correct = 0;
		timexCounter = 0;
		timeCounter = 0;
		setCounter = 0;
		durationCounter = 0;
		dateCounter = 0;
		timeCorrect = 0;
		setCorrect = 0;
		durationCorrect = 0;
		dateCorrect = 0;
	}

	/*
	 * This is called ONCE for each document
	 */
	@Override
	public void process(JCas jcas) throws AnalysisEngineProcessException {

		Collection<TimexEvent> timexEvents = JCasUtil.select(jcas, TimexEvent.class);
		for (TimexEvent timexEvent : timexEvents) {
			System.out.println(timexEvent.getGoldValue() + " detected as " + timexEvent.getDetectedValue() + ".");
			timexCounter++; 
			if (timexEvent.getDetectedValue().equals(timexEvent.getGoldValue())) {
				correct++;
			}
			if (timexEvent.getTimexMode().equals("TIME")){
				timeCounter++;
				if (timexEvent.getDetectedValue().equals(timexEvent.getGoldValue())) {
					timeCorrect++;
				}
			}
			if (timexEvent.getTimexMode().equals("SET")){
				setCounter++;
				if (timexEvent.getDetectedValue().equals(timexEvent.getGoldValue())) {
					setCorrect++;
				}
			}
			if (timexEvent.getTimexMode().equals("DURATION")){
				durationCounter++;
				if (timexEvent.getDetectedValue().equals(timexEvent.getGoldValue())) {
					durationCorrect++;
				}
			}
			if (timexEvent.getTimexMode().equals("DATE")){
				dateCounter++;
				if (timexEvent.getDetectedValue().equals(timexEvent.getGoldValue())) {
					dateCorrect++;
				}
			}
		}
	}

	/*
	 * This is called AFTER all documents have been processed.
	 */
	@Override
	public void collectionProcessComplete()
			throws AnalysisEngineProcessException {
		super.collectionProcessComplete();
		float percent = correct * 100f / timexCounter;
		float datePercent = dateCorrect * 100f / dateCounter;
		float durationPercent = durationCorrect * 100f / durationCounter;
		float timePercent = timeCorrect * 100f / timeCounter;
		float setPercent = setCorrect * 100f / setCounter;
		
		System.out.println("______________________________________________________________");
		System.out.println("EVALUATION:");
		System.out.println("CAS found " + timexCounter + " timex tags.");
		System.out.println(dateCounter + " were of type 'DATE', " + durationCounter + " were of type 'DURATION', " + setCounter + " were of type 'SET', " + timeCounter + " were of type 'TIME'.");
		System.out.println(correct + " out of " + timexCounter + " time expressions were detected correctly. This is a success rate of " + percent + " %.");
		System.out.println("Precision 'DATE': " + datePercent+ " %.");
		System.out.println("Precision 'DURATION': " + durationPercent+ " %.");
		System.out.println("Precision 'TIME': " + timePercent+ " %.");
		System.out.println("Precision 'SET': " + setPercent+ " %.");
	}
}